package org.komarichin.jdo.dao;

import org.apache.commons.lang.StringUtils;
import org.komarichin.beans.IFile;
import org.komarichin.beans.dao.IFileDAO;
import org.komarichin.jdo.persistence.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * User: Mykhaylo Komarichyn
 * Date: 31.12.2009
 * Time: 10:53:56
 */
@Transactional(readOnly = true)
public class FileDAO extends JdoGenericDAOSupport<File, Long> implements IFileDAO<File, Long> {
    private static final long serialVersionUID = -2327337287013241237L;
    private static final Logger log = LoggerFactory.getLogger(FileDAO.class);

    public FileDAO(Class<File> type) {
        super(type);
    }

    @Override
    public File newInstance() {
        log.debug("crate new instance file");
        return new File();
    }

    @Override
    public IFile save(File file, File folder) {
        log.info("save# file:{} and folder:{}", file, folder);
        file.setFolder(folder.getFolder());
        return save(file);
    }

    @Override
    public Collection<File> getFiles(String folderName) {
        log.debug("get files in folder: {}", folderName);
        if (StringUtils.isEmpty(folderName)) {
            log.warn("folder name can't be empty");
            return new ArrayList<File>();
        }
        Collection<File> files = getJdoTemplate().find(getType(), "folder == folderPath && length > 0", "String folderPath", folderName);
        files.size();
        log.debug("result size:{}", files.size());
        return files;
    }

    @Override
    public Collection<File> getFilesAndFolders(String folderName) {
        log.debug("get files and folders by path:{}", folderName);
        if (StringUtils.isEmpty(folderName)) {
            log.warn("folder name can't be empty");
            return new ArrayList<File>();
        }
        Collection<File> filesAndFolder = getJdoTemplate().find(File.class, "folder == folderPath", "String folderPath", folderName);
        filesAndFolder.size();
        log.debug("result size: {}", filesAndFolder.size());
        return filesAndFolder;
    }

    @Override
    @Transactional(readOnly = false)
    public File save(File file) {
        log.info("save# file:{}", file);
        if (file == null || StringUtils.isEmpty(file.getName()) || StringUtils.isEmpty(file.getFolder())) {
            return null;
        }
        file.setUpdate(new Date());
        return super.save(file);
    }


    @Override
    @Transactional(readOnly = false)
    public Long saveOrUpdate(File file) {
        log.info("save or update# file:{}", file);
        if (file == null || StringUtils.isEmpty(file.getName()) || StringUtils.isEmpty(file.getFolder())) {
            log.warn("file can't be null or file name can't be null or file's folder can't be null");
            return null;
        }
        IFile f = getJdoTemplate().getObjectById(File.class, file.getId());
        if (f == null) {
            getJdoTemplate().makePersistent(file);
        } else {
            f.setUpdate(new Date());
            f.setContentType(file.getContentType());
            f.setFile(file.getFile());
            f.setFolder(file.getFolder());
            f.setLength(file.getLength());
            f.setName(file.getName());
            f.setType(file.getType());
        }
        return file.getId();
    }

    @Override
    public boolean exists(String fileName, String folder) {
        log.debug("file: {} exists in folder: {}", fileName, folder);
        if (StringUtils.isNotEmpty(fileName) && StringUtils.isNotEmpty(folder))
            for (IFile file : getFiles(folder)) {
                if (fileName.equals(file.getName())) return true;
            }
        return false;
    }

    @Override
    public boolean isFolderEmpty(String folder) {
        log.debug("is folder: {}  empty", folder);
        return !(getFiles(folder).size() > 0);
    }

    @Override
    public Collection<String> getFolders(String currentFolder) {
        log.debug("get folders in folder: {}", currentFolder);
        if (StringUtils.isEmpty(currentFolder)) {
            log.warn("folder name can't be empty");
            return new ArrayList<String>();
        }
        Collection<File> files = getJdoTemplate().find(getType(), "folder == folderPath && file == null", "String folderPath", currentFolder);
        files.size();
        Collection<String> result = new ArrayList<String>();
        for (IFile file : files) {
            if (file.isDirectory())
                result.add(file.getName());
        }
        log.debug("result size:{}", result.size());
        return result;
    }
}
